package com.yungu.swift.admin.route.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Lists;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.common.model.param.AdminConfigParam;
import com.yungu.swift.common.model.vo.ConfigVo;
import com.yungu.swift.common.service.CommonConfigService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.yungu.swift.admin.config.ApplicationConfig.*;

/**
 * @author : cuixiuyin
 * @date : 2019/6/17
 */
@RestController
@RequestMapping("/admin/v1/system/config")
@Api(value = "【系统服务-业务设置中心】", tags = "App功能配置")
public class ConfigAdmin {

    @Reference
    private CommonConfigService commonConfigService;

    @RequestMapping(value = "/listConfig", method = RequestMethod.GET)
    @ApiOperation(value = "App功能配置获取", httpMethod = "GET")
    public ResponseData<List<ConfigVo>> listConfig(@RequestBody AdminParam adminParam) throws JsonProcessingException {
        // 系统通用配置
        ConfigVo commonConfig = new ConfigVo();
        commonConfig.setAppid(adminParam.getAppid());
        commonConfig.setConfigKey(CommonConstant.COMMON_CONFIG);
        commonConfig.setConfigValue(JsonUtils.writeJsonStr(COMMON_CONFIG));
        // 系统乘客端配置
        ConfigVo pasConfig = new ConfigVo();
        pasConfig.setAppid(adminParam.getAppid());
        pasConfig.setConfigKey(CommonConstant.PASSENGER_CONFIG);
        pasConfig.setConfigValue(JsonUtils.writeJsonStr(PASSENGER_CONFIG));
        // 系统司机端配置
        ConfigVo driConfig = new ConfigVo();
        driConfig.setAppid(adminParam.getAppid());
        driConfig.setConfigKey(CommonConstant.DRIVER_CONFIG);
        driConfig.setConfigValue(JsonUtils.writeJsonStr(DRIVER_CONFIG));
        // 系统服务端配置
        ConfigVo serverConfig = new ConfigVo();
        serverConfig.setAppid(adminParam.getAppid());
        serverConfig.setConfigKey(CommonConstant.SERVER_CONFIG);
        serverConfig.setConfigValue(JsonUtils.writeJsonStr(SERVER_CONFIG));
        return ResponseData.buildSuccessResponse(Lists.newArrayList(commonConfig, pasConfig, driConfig, serverConfig));
    }

    @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> listConfig(@RequestBody AdminConfigParam adminConfigParam) {
        if (CollectionUtils.isEmpty(adminConfigParam.getConfigAddParamList())) {
            return ResponseData.buildSuccessResponse(Boolean.FALSE);
        }
        return commonConfigService.addOrUpdateConfig(adminConfigParam);
    }
}
